package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper , Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品和口味信息
     * @param dishDto
     */
    @Override
    @Transactional //(多表)开启事务
    public void dishWithFlavor(DishDto dishDto) {
        //添加菜品信息
        save(dishDto);
        //遍历集合吗,添加口味信息
        //补充dishId字段
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
                });
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询回显数据
     * @param id
     * @return
     */
    @Override
    public DishDto getWithFlavor(Long id) {
        //菜品信息
        Dish dish = getById(id);

        DishDto dishDto = new DishDto();
        //拷贝数据
        BeanUtils.copyProperties(dish,dishDto);
        //构建条件器
        LambdaQueryWrapper<DishFlavor> dlqw = new LambdaQueryWrapper<>();
        //查询菜品口味
        dlqw.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(dlqw);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 修改菜品和口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //1修改菜品信息
        updateById(dishDto);
        //删除修改之前的口味信息
        LambdaQueryWrapper<DishFlavor> dlqw = new LambdaQueryWrapper<>();
        dlqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dlqw);
        //新增修改之后的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
        });
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品和口味信息
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithFlavor(List<Long> ids) {
        //判断菜品是否在起售状态
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.in(Dish::getId,ids);
        dlqw.eq(Dish::getStatus,1);
        int count = count(dlqw);
        if (count>0){
            //菜品正在售卖,无法删除
            throw new CustomException("菜品正在售卖,无法删除");
        }
        //菜品不在售卖,可以删除
        //删除菜品信息
        removeByIds(ids);
        //删除口味数据
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lqw);
    }
}
